package com.quectel.core.module.device.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.device.dao.DeviceCommandTemplateDao;
import com.quectel.core.module.device.dto.DeviceCommandTemplateDto;
import com.quectel.core.module.device.entity.DeviceCommandTemplateEntity;
import com.quectel.core.module.device.service.DeviceCommandTemplateService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-05-24 12:42:22
 */
@DubboService
public class DeviceCommandTemplateServiceImpl implements DeviceCommandTemplateService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceCommandTemplateServiceImpl.class);

    @Autowired
    private DeviceCommandTemplateDao deviceCommandTemplateDao;


    @Override
    public DeviceCommandTemplateDto selectByByCode(String commandCode) {
        DeviceCommandTemplateEntity entity = deviceCommandTemplateDao
                .selectOne(new QueryWrapper<DeviceCommandTemplateEntity>().eq("command_code", commandCode).last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(entity, DeviceCommandTemplateDto.class);
    }

    @Override
    public List<DeviceCommandTemplateDto> queryList(Map<String, Object> params) {
        List<DeviceCommandTemplateEntity> list = deviceCommandTemplateDao.queryList(params);
        return CopyUtils.copyList(list, DeviceCommandTemplateDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deviceCommandTemplateDao.queryTotal(params);
    }

    @Override
    public Long save(DeviceCommandTemplateDto dto) {
        DeviceCommandTemplateEntity entity = CopyUtils.copyObj(dto, DeviceCommandTemplateEntity.class);
        entity.setId(Snowflake.nextId());
        deviceCommandTemplateDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeviceCommandTemplateDto dto) {
        DeviceCommandTemplateEntity entity = CopyUtils.copyObj(dto, DeviceCommandTemplateEntity.class);
        deviceCommandTemplateDao.updateById(entity);
    }

    @Override
    public void forceUpdateColumnById(Map<String, Object> setParams, Long id) {
        if (setParams == null || setParams.size() <= 0) {
            return;
        }
        UpdateWrapper<DeviceCommandTemplateEntity> updateMapper = new UpdateWrapper<>();
        for (Map.Entry<String, Object> entry : setParams.entrySet()) {
            updateMapper.set(entry.getKey(), entry.getValue());
        }
        //计算主键列的名字
        String primaryKeyName = null;
        Class<DeviceCommandTemplateEntity> clazz = DeviceCommandTemplateEntity.class;
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            TableId annotation = declaredField.getAnnotation(TableId.class);
            if (annotation != null) {
                primaryKeyName = declaredField.getName();
                break;
            }
        }
        updateMapper.eq(primaryKeyName == null ? "id" : StrUtil.toUnderlineCase(primaryKeyName), id);

        deviceCommandTemplateDao.update(new DeviceCommandTemplateEntity(), updateMapper);
    }

    @Override
    public DeviceCommandTemplateDto selectById(Long id) {
        DeviceCommandTemplateEntity entity = deviceCommandTemplateDao.selectById(id);
        return CopyUtils.copyObj(entity, DeviceCommandTemplateDto.class);
    }

    @Override
    public void deleteById(Long id) {
        deviceCommandTemplateDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
